require.config({
               baseUrl:"../",
               paths:{
               bsl:'bsl',
               
               }
               });
//        alert("baseUrl")
//
require(['bsl'],function(bsl){
        document.addEventListener('deviceready', onDeviceready, false);
        function onDeviceready() {
        var LOG="";
        alert("IO");
        var showInfo=document.getElementById("LOG");
        
        function CreateFileAndDircetory(){
        LOG="Create File And Dircetory<br>";
        
        function onError(err){
        LOG=LOG+"error<br>";
        showInfo.innerHTML=LOG;
        }
        var src="gggggg.txt";
        var dir="kk";
        
        alert(LocalFileSystem.PERSISTENT);
        bsl.io.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
                                 alert(fileSystem.name+"fffff"+fileSystem.root.name);
                                 fileSys=fileSystem;
                                 fileSystem.root.getFile(src, {
                                                         create: true,
                                                         exclusive:false
                                                         }, function(fileEntry) {
                                                         
                                                         //                                                         alert("File " + src + " created at " + fileEntry.fullPath+"jjjjj"+fileEntry.nativeURL);
                                                         LOG=LOG+"File " + src + " created at fullPath:" + fileEntry.fullPath+"nativeURL:"+fileEntry.nativeURL+"<br>";
                                                         showInfo.innerHTML=LOG;
                                                         
                                                         }, onError);
                                 fileSystem.root.getDirectory(dir,{
                                                              create: true,
                                                              exclusive: false
                                                              },function(fileEntry) {
                                                              
                                                              //                                                              alert("directory " + src + " created at fullPath:" + fileEntry.fullPath+"nativeURL:"+fileEntry.nativeURL+"<br>");
                                                              LOG=LOG+"directory " + src + " created at fullPath:" + fileEntry.fullPath+"nativeURL:"+fileEntry.nativeURL+"<br>";
                                                              showInfo.innerHTML=LOG;
                                                              
                                                              
                                                              }, onError);
                                 
                                 },onError);
        }
        
        function doAppendFile() {
        
        LOG="doAppendFile<br>";
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
                                 fileSystem.root.getFile("gggggg.txt", {
                                                         create: true
                                                         }, function(fileEntry) {
                                                         
                                                         fileEntry.file(gotFile, function(){
                                                                        alert("fileEntry.file.error")
                                                                        });
                                                         function gotFile(file){
                                                         //                                                         alert("file.size:"+file.size+"file.name:"+file.name+"file.localURL:"+file.localURL+"file.type"+file.type+"file.lastModifiedDate:"+ file.lastModifiedDate)
                                                         LOG=LOG+"file.size"+file.size+"<br>file.name:"+file.name+"<br>file.localURL:"+file.localURL+"<br>file.type"+file.type+"<br>file.lastModifiedDate:"+ file.lastModifiedDate+"<br>"
                                                         showInfo.innerHTML=LOG;
                                                         readDataUrl(file);
                                                         readAsText(file);
                                                         }
                                                         
                                                         function readDataUrl(file) {
                                                         var reader = new bsl.io.FileReader();
                                                         reader.onloadend = function(evt) {
                                                         //                                                         console("read"+evt+"evt.target"+evt.target+"Read as data URL"+evt.target.result);
                                                         LOG=LOG+"read"+evt+"<br>evt.target:"+JSON.stringify(evt.target)+"<br>Read as data URL:"+evt.target.result+"<br>"
                                                         
                                                         showInfo.innerHTML=LOG;
                                                         };
                                                         reader.readAsDataURL(file);
                                                         }
                                                         
                                                         function readAsText(file) {
                                                         var reader = new FileReader();
                                                         reader.onloadend = function(evt) {
                                                         //                                                         alert("Read as text"+evt.target.result);
                                                         LOG=LOG+"read"+evt+"<br>evt.target:"+JSON.stringify(evt.target)+"<br>Read as text:"+evt.target.result+"<br>"
                                                         
                                                         showInfo.innerHTML=LOG;
                                                         };
                                                         reader.readAsText(file);
                                                         }
                                                         
                                                         fileEntry.createWriter(function(writerOb) {
                                                                                writerOb.onwrite = function() {
                                                                                
                                                                                }
                                                                                //go to the end of the file...
                                                                                writerOb.seek(writerOb.length);
                                                                                writerOb.write("Test at " + "good day" +"\n");
                                                                                LOG=LOG+"write"+"good day"+"<br>"
                                                                                showInfo.innerHTML=LOG;
                                                                                }
                                                                                
                                                                                )
                                                         
                                                         
                                                         },  function(e) {
                                                         alert("onError" + e);
                                                         
                                                         })
                                 
                                 
                                 // move the file to a new directory and rename it
                                 
                                 
                                 },function(e){
                                 alert("onError" + e);
                                 })
        };
        
        
        function copyfile(){
        LOG="copyfile";
        var fromUrl=bsl.io.documentsDirectory+"gggggg.txt";
        //        var  fromUrl=bsl.io.documentsDirectory;
        
        var toName="kk.txt";
        LOG=LOG+"copyFile - From: [" + fromUrl + "] To:  " + toName + "<br>";
        showInfo.innerHTML=LOG;
        bsl.io.resolveLocalFileSystemURI(fromUrl,function(entry){
                                         //entri.getParent(function(entry){
                                         bsl.io.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
                                                                  function (fileSys){
                                                                  fileSys.root.getDirectory("HHH", {create: true, exclusive:false}, function (directory){
                                                                                            // Get the destination file name, set it if it is blank or not passed by the App
                                                                                            alert(directory.fullPath+directory.nativeURL);
                                                                                            // Remember the full path name for the console log
                                                                                            directory.fullPath=""
                                                                                            directory.nativeURL=cordova.file.documentsDirectory;
                                                                                            entry.copyTo(directory, toName, function (){
                                                                                                         LOG= LOG+"Successful copy of "+ " to " + fullDestPath+"<br>";
                                                                                                         showInfo.innerHTML=LOG;
                                                                                                         }, function (error){
                                                                                                         LOG= LOG +"copyError code: " + JSON.stringify(error)+"</br>";
                                                                                                         showInfo.innerHTML=LOG;                                 });
                                                                                            
                                                                                            
                                                                                            }, function (error){
                                                                                            alert("getDestDirError code: " + JSON.stringify(error));
                                                                                            LOG= LOG +"getDestDirError code:: " + JSON.stringify(error)+"</br>";
                                                                                            showInfo.innerHTML=LOG;
                                                                                            });
                                                                  
                                                                  
                                                                  }, function (error){
                                                                  alert("requestFileSystemError code: ");
                                                                  });
                                         //},function(error){
                                         //        alert("getparentError");
                                         //    });
                                         
                                         }, function(error){
                                         alert("resolveLocalFileSystemURI code: ");
                                         });
        
        
        
        }
        function getDirectory(){
        
        bsl.io.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
                                 function (fileSys){
                                 fileSys.root.getDirectory('NoCloud', {}, function(dirEntry){
                                                           var dirReader = dirEntry.createReader();
                                                           dirReader.readEntries(function(entries) {
                                                                                 for(var i = 0; i < entries.length; i++) {
                                                                                 var entry = entries[i];
                                                                                 if (entry.isDirectory){
                                                                                 alert('Directory: ' + entry.fullPath);
                                                                                 }
                                                                                 else if (entry.isFile){
                                                                                 alert('File: ' + entry.fullPath);
                                                                                 }
                                                                                 }
                                                                                 
                                                                                 },function(error){ alert("readEntries error")
                                                                                 });
                                                           },function(error){alert("getDirectory error")}
                                                           )},
                                 function(error){
                                 
                                 alert("requestFileSystem error")
                                 });
        
        
        
        
        }
        function getDirectoryInOtherWay(){
        
        var fromUrl=cordova.file.documentsDirectory
        
        bsl.io.resolveLocalFileSystemURI(fromUrl,function(entry){
                                         
                                         var dirReader = entry.createReader();
                                         dirReader.readEntries(function(entries) {
                                                               for(var i = 0; i < entries.length; i++) {
                                                               var entry = entries[i];
                                                               if (entry.isDirectory){
                                                               alert('Directory: ' + entry.fullPath);
                                                               }
                                                               else if (entry.isFile){
                                                               alert('File: ' + entry.fullPath);
                                                               }
                                                               }
                                                               
                                                               },function(error){ alert("readEntries error")
                                                               });
                                         },function(){
                                         alert("resolveLocalFileSystemURI");
                                         })
        };
        
        function removeFile(){
        
        LOG="removeFile gggggg.txt <br>";
        
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
                                 fileSystem.root.getFile("gggggg.txt", {
                                                         }, function(fileEntry) {
                                                         
                                                         fileEntry.remove(
                                                                          function (entry) {
                                                                          alert("Removal succeeded");
                                                                          },
                                                                          function (error) {
                                                                          alert("Error removing file:" + error.code);
                                                                          });
                                                         },function(error){
                                                         alert("Error getFile :"+ error.code);
                                                         })
                                 }, function(){
                                 alert("Error requestFileSystem :"+ error.code);
                                 
                                 });
        
        };
        
        document.getElementById('CreateFileAndDircetory').onclick=CreateFileAndDircetory;
        document.getElementById('doAppendFile').onclick=doAppendFile;
        document.getElementById('copyfile').onclick=copyfile;
        document.getElementById('getDirectory').onclick=getDirectory;
        document.getElementById('getDirectoryInOtherWay').onclick=getDirectoryInOtherWay;
        document.getElementById('removeFile').onclick=removeFile;
        
        
        }
        
        
        
        
        
        
        
        })